package com.lyhengtongwl.zqsnews.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.TaskEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.ui.activity.NewsAuthenticationActivity;
import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;
import com.lyhengtongwl.zqsnews.utils.AppConfig;
import com.lyhengtongwl.zqsnews.utils.CheckApkExist;
import com.lyhengtongwl.zqsnews.utils.CopyUtils;
import com.lyhengtongwl.zqsnews.utils.FTYUtils;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.widget.NewsShare;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.util.List;

import retrofit2.Response;

public class TaskAdapter extends BaseQuickAdapter<TaskEntity, BaseViewHolder> {
    private Activity activity;
    private FragmentManager fragmentManager;

    public TaskAdapter(Activity activity, FragmentManager fragmentManager, List<TaskEntity> newsList) {
        super(R.layout.item_task, newsList);
        this.activity = activity;
        this.fragmentManager = fragmentManager;
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, TaskEntity item) {
        Picasso.with(App.getContext()).load(item.getImgUrl()).into((ImageView) helper.getView(R.id.iv_img));
        ((TextView) helper.getView(R.id.tv_title)).setText(item.getTitle());
        ((TextView) helper.getView(R.id.tv_content)).setText(item.getDescription());
        ((TextView) helper.getView(R.id.tv_money)).setText(
                new BigDecimal(FTYUtils.fenToYuan(item.getMoney())).stripTrailingZeros().toPlainString() + "元");
        TextView tv = ((TextView) helper.getView(R.id.tv_state));
        String state = item.getState();
        String taskID = item.getTaskType();
        switch (state) {//任务状态
            case "0":
                tv.setText("去完成");
                tv.setTextColor(activity.getResources().getColor(R.color.mainBg));
                tv.setBackground(activity.getResources().getDrawable(R.drawable.task_selected_shape));
                break;
            case "1":
                tv.setText("领取奖励");
                tv.setTextColor(activity.getResources().getColor(R.color.mainBg));
                tv.setBackground(activity.getResources().getDrawable(R.drawable.task_selected_shape));
                break;
            case "2":
                tv.setText("已完成");
                tv.setTextColor(activity.getResources().getColor(R.color.colorCCC));
                tv.setBackground(activity.getResources().getDrawable(R.drawable.task_unselected_shape));
                break;

        }
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (taskID) {
                    case "1"://关注公众号
                        if ("0".equals(state)) {//表示可以进行关注
                            CopyUtils.copyText(activity, AppConfig.getInstance(App.getContext()).get("wxNumber"));
                            openWX();
                            String userId = SPUtils.get(App.getContext(), "userId", "0") + "";
                            Task.getApiService().toComplete(userId, taskID).enqueue(new MyCallback<NewsBaseEntity>(activity) {
                                @Override
                                protected void onSuccess(Response<NewsBaseEntity> response) {

                                }

                                @Override
                                protected void onFailure(Throwable t) {

                                }
                            });


                        } else if ("1".equals(state)) {//表示领取奖励
                            receiveAward(tv, Integer.parseInt(item.getTaskType()), item);
                        } else {
                            NewsToastUtils.showToast(activity, "已经完成任务啦~");
                        }
                        break;
                    case "2"://邀请好友
                        if ("0".equals(state)) {//表示可以进行关注
                            NewsShare.showShareDialog_1(fragmentManager, activity);
                        } else if ("1".equals(state)) {//表示领取奖励
                            receiveAward(tv, Integer.parseInt(item.getTaskType()), item);
                        } else {
                            NewsToastUtils.showToast(activity, "已经完成任务啦~");
                        }
                        break;
                    case "3"://分享文章
                        if ("0".equals(state)) {//表示可以进行关注
                            ((MainActivity) activity).getTabLayout(0).setCurrentTab(0);
                        } else if ("1".equals(state)) {//表示领取奖励
                            receiveAward(tv, Integer.parseInt(item.getTaskType()), item);
                        } else {
                            NewsToastUtils.showToast(activity, "已经完成任务啦~");
                        }
                        break;
                    case "4"://实名认证
                        if ("0".equals(state)) {//表示可以进行关注
                            activity.startActivity(new Intent(activity, NewsAuthenticationActivity.class));
                        } else if ("1".equals(state)) {//表示领取奖励
                            receiveAward(tv, Integer.parseInt(item.getTaskType()), item);
                        } else {
                            NewsToastUtils.showToast(activity, "已经完成任务啦~");
                        }
                        break;
                }
            }
        });

    }

    //领取奖励
    private void receiveAward(TextView tv, int taskId, TaskEntity item) {
        int userId = Integer.parseInt(SPUtils.get(App.getContext(), "userId", "0") + "");
        Task.getApiService().receiveAward(taskId, userId).enqueue(new MyCallback<NewsBaseEntity>(activity) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    toast(item);
                    tv.setText("已完成");
                    tv.setTextColor(activity.getResources().getColor(R.color.colorCCC));
                    tv.setBackground(activity.getResources().getDrawable(R.drawable.task_unselected_shape));
                } else {
                    NewsToastUtils.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                NewsToastUtils.showToast(activity, "请求失败！");
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void toast(TaskEntity item) {
        Toast toast2 = new Toast(activity);
        View view = LayoutInflater.from(activity).inflate(R.layout.toast_getmoney, null);
        TextView tv1 = view.findViewById(R.id.tv_1);
        TextView tv2 = view.findViewById(R.id.tv_2);
        TextView tv3 = view.findViewById(R.id.tv_3);
        tv1.setText("恭喜您，获得");
        tv2.setText(new BigDecimal(FTYUtils.fenToYuan(item.getMoney())).stripTrailingZeros().toPlainString() + "元");
        tv3.setText("现金奖励！");
        toast2.setView(view);
        toast2.setGravity(Gravity.CENTER, 0, 0);
        toast2.show();
    }
    //打开微信
    public void openWX() {
        if (CheckApkExist.checkApkExist(activity, "com.tencent.mm")) {
            Intent intent = new Intent();
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            activity.startActivity(intent);
        } else {
            NewsToastUtils.showToast(activity, "未安装手微信或安装的版本不支持！");
        }
    }
}
